#!/usr/bin/env bash

# Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

OHOS_SDK=$1
OFFICIAL_MUSL_DIR=$2
DESTDIR=$3

if [ -z "$OHOS_SDK" ]; then
    echo "Please specify the path of OHOS SDK."
    exit 1
fi

if [ -z "$OFFICIAL_MUSL_DIR" ]; then
    echo "Please specify the path of prebuilt official musl libs."
    exit 1
fi

if [ -z "$DESTDIR" ]; then
    echo "Please specify the destination directory."
    exit 1
fi

triple=aarch64-linux-ohos
mkdir -p $DESTDIR/usr/include || exit 1
mkdir -p $DESTDIR/usr/lib/${triple} || exit 1

for i in arpa net netinet netpacket scsi sys fortify; do
    cp -rP $OHOS_SDK/native/sysroot/usr/include/$i $DESTDIR/usr/include/$i || exit 1
done
# musl has no sys/capability.h, and ohos 4.1 musl has only 2 functions in it, use libcap instead
rm $DESTDIR/usr/include/sys/capability.h || exit 1

# TODO: arm
cp -rP $OHOS_SDK/native/sysroot/usr/include/aarch64-linux-ohos/bits $DESTDIR/usr/include/bits || exit 1

outer_incs=(\
    aio.h       crypt.h   errno.h     ftw.h       iso646.h    locale.h    netdb.h     regex.h      signal.h       stdint.h       stropts.h   threads.h   utmp.h \
    alloca.h    ctype.h   fcntl.h     getopt.h    langinfo.h  malloc.h    nl_types.h  resolv.h     spawn.h        stdio_ext.h    syscall.h   time.h      utmpx.h \
    ar.h        dirent.h  features.h  glob.h      lastlog.h   math.h      paths.h     sched.h      stdalign.h     stdio.h        sysexits.h  uchar.h     values.h \
    assert.h    dlfcn.h   fenv.h      grp.h       libgen.h    memory.h    poll.h      search.h     stdarg.h       stdlib.h       syslog.h    ucontext.h  wait.h \
    byteswap.h  elf.h     float.h     iconv.h     libintl.h   mntent.h    pthread.h   semaphore.h  stdbool.h      stdnoreturn.h  tar.h       ulimit.h    wchar.h \
    complex.h   endian.h  fmtmsg.h    ifaddrs.h   limits.h    monetary.h  pty.h       setjmp.h     stdc-predef.h  string.h       termios.h   unistd.h    wctype.h \
    cpio.h      err.h     fnmatch.h   inttypes.h  link.h      mqueue.h    pwd.h       shadow.h     stddef.h       strings.h      tgmath.h    utime.h     wordexp.h \
)
extra_outer_incs=(execinfo.h) # ohos musl has bundled execinfo.h
for i in "${outer_incs[@]}" "${extra_outer_incs[@]}"; do
    case $i in
        libintl.h|crypt.h)
            # FIXME: ohos 4.1 musl has no functions in libintl.h, use gettext instead
            # FIXME: ohos 4.1 musl has no functions in crypt.h
            ;;
        *)
            cp -P $OHOS_SDK/native/sysroot/usr/include/$i $DESTDIR/usr/include/$i || exit 1
            ;;
    esac
done

libs=(\
    crt1.o  crti.o  crtn.o  \
    libc.a  libcrypt.a  libc.so  libdl.a  libm.a  libpthread.a  libresolv.a  librt.a  libutil.a  libxnet.a  rcrt1.o  Scrt1.o \
)
for i in "${libs[@]}"; do
    cp -P $OHOS_SDK/native/sysroot/usr/lib/aarch64-linux-ohos/$i $DESTDIR/usr/lib/${triple}/$i || exit 1
    ln -sf ${triple}/$i $DESTDIR/usr/lib/$i || exit 1
done

mkdir -p $DESTDIR/lib || exit 1
ln -sf /usr/lib/libc.so $DESTDIR/lib/ld-musl-aarch64.so.1 || exit 1

# FIXME: musl-ohos can not run on qemu, so we use official musl instead
mkdir -p $DESTDIR/usr/gnemul/qemu-aarch64/lib || exit 1
ln -f $OFFICIAL_MUSL_DIR/usr/lib/libc.so $DESTDIR/usr/gnemul/qemu-aarch64/lib/ld-musl-aarch64.so.1 || exit 1

for i in /etc/ferrium/pkgs/musl-ohos-1.2.3/post-patches/*.patch; do
    patch -d $DESTDIR -p1 < $i || exit 1
done
